package com.telchina.workorder.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.telchina.common.annotation.Log;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.domain.WoAttachment;
import com.telchina.common.enums.BusinessType;
import com.telchina.common.mapper.WoAttachmentMapper;
import com.telchina.common.utils.DateUtils;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.common.utils.poi.ExcelUtil;
import com.telchina.workorder.domain.WoSupervise;
import com.telchina.workorder.domain.vo.WoSuperviseVO;
import com.telchina.workorder.service.IWoSuperviseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 督办申请Controller
 *
 * @author konghao
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/workorder/woSupervise")
public class WoSuperviseController extends BaseController {
    @Autowired
    private IWoSuperviseService woSuperviseService;
    @Autowired
    private WoAttachmentMapper woAttachmentMapper;

    /**
     * 分页查询督办申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:list')")
    @GetMapping("/list")
    public AjaxResult list(WoSupervise woSupervise) {
        startPage();
//        IPage<WoSupervise> list = woSuperviseService.selectList(startPage(), woSupervise);
        List<WoSuperviseVO> list = woSuperviseService.selectListAll(woSupervise);
        return getDataTable(list);
    }

    /**
     * 查询督办申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll(WoSupervise woSupervise) {
        List<WoSuperviseVO> listVO = woSuperviseService.selectListAll(woSupervise);
        // 获取对应的附件信息
        List<String> superviseIds = listVO.stream().map(WoSupervise::getId).collect(Collectors.toList());
        List<WoAttachment> woAttachmentList = Collections.emptyList();
        if (CollUtil.isNotEmpty(superviseIds)) {
            woAttachmentList = woAttachmentMapper.selectList(new QueryWrapper<WoAttachment>()
                    .lambda().in(WoAttachment::getBussId, superviseIds));
        }
        List<WoAttachment> finalWoAttachmentList = woAttachmentList;
        listVO.stream().forEach(A -> {
            A.setWoAttachmentList(finalWoAttachmentList.stream().filter(B -> A.getId().equals(B.getBussId()))
                    .collect(Collectors.toList()));
        });
        return AjaxResult.success(listVO);
    }

    /**
     * 导出督办申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:export')")
    @Log(title = "督办申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WoSupervise woSupervise) {
        List<WoSuperviseVO> list = woSuperviseService.selectListAll(woSupervise);
        ExcelUtil<WoSuperviseVO> util = new ExcelUtil<WoSuperviseVO>(WoSuperviseVO.class);
        return util.exportExcel(list, "supervise");
    }

    /**
     * 获取督办申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(woSuperviseService.selectById(id));
    }

    /**
     * 新增督办申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:add')")
    @Log(title = "督办申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WoSupervise woSupervise) throws Exception {
        return woSuperviseService.insert(woSupervise);
    }

    /**
     * 修改督办申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:edit')")
    @Log(title = "督办申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WoSupervise woSupervise) {
        return toAjax(woSuperviseService.update(woSupervise));
    }

    /**
     * 删除督办申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:remove')")
    @Log(title = "督办申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(woSuperviseService.deleteByIds(ids));
    }

    /**
     * 获取督办申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:supervise:add')")
    @GetMapping(value = "/getInitSupervise")
    public AjaxResult getInitExtension() {
        Map<String, Object> result = new HashMap<>();
        // 获取登录人所在部门信息
        result.put("id", UUID.fastUUID().toString(Boolean.TRUE));
        result.put("filledTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
        result.put("superviseNo", DateUtils.parseDateToStr("yyMMdd", new Date()));
        result.put("filledBy", SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success(result);
    }

    /**
     * 工作流程相关接口开始
     */
    /**
     * 分页查询工单信息我的代办列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @GetMapping("todoList")
    public AjaxResult todoList(WoSupervise supervise) {
        List<WoSuperviseVO> list = woSuperviseService.selectTodoList(supervise);
        return getDataTable(list);
    }

    /**
     * 审核处理
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:add')")
    @Log(title = "审核处理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/approve")
    public AjaxResult approveExtension(@RequestBody Map<String, Object> maps) throws Exception {
        return woSuperviseService.approveSupervise(maps);
    }
}
